package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/3/12 14:33
 * @description：https://leetcode-cn.com/problems/n-ary-tree-postorder-traversal/
 */
public class N叉树的后序遍历 {
    @Test
    public void test() {
        Solution solution = new Solution();
    }

    class Node {
        public int val;
        public List<Node> children;

        public Node() {
        }

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }


    class Solution {
        public List<Integer> postorder(Node root) {
            List<Integer> ans = new ArrayList<>();
            order(root, ans);
            return ans;
        }

        private void order(Node node, List<Integer> ans) {
            if (node == null) return;
            if (node.children != null) {
                for (Node child : node.children) {
                    order(child, ans);
                }
            }
            ans.add(node.val);
        }
    }

}
